<!--
	Copyright (C) 2010 Ethan Gruber
	EADitor: http://code.google.com/p/eaditor/
	Apache License 2.0: http://code.google.com/p/eaditor/
	
	Delete selected EAD guide from eXist (or other interface).  This is linked from the file list page -->
<html xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xforms="http://www.w3.org/2002/xforms"
	xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms"
	xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:exist="http://exist.sourceforge.net/NS/exist"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:fr="http://orbeon.org/oxf/xml/form-runner" xmlns="http://www.w3.org/1999/xhtml"
	xmlns:widget="http://orbeon.org/oxf/xml/widget">
	<head>
		<title>EADitor: Publish</title>
		<link type="text/css" href="http://localhost:8080/cocoon/numishare/xforms.css"
			rel="stylesheet"/>
		<xforms:model>
			<xforms:instance id="id">
				<!-- Start with placeholders for three files -->
				<id xmlns=""/>
			</xforms:instance>

			<!-- load URLs from site_info.xml in eXist into form for Solr and CSS file -->
			<xforms:instance id="site-info">
				<site_info xmlns="">
					<url/>
					<solr_url/>
				</site_info>
			</xforms:instance>

			<!-- send to Solr -->
			<xforms:instance id="addIndex">
				<add xmlns=""/>
			</xforms:instance>

			<!-- delete from Solr -->
			<xforms:instance id="delete-all">
				<delete>
					<query>*:*</query>
				</delete>
			</xforms:instance>

			<!-- Solr optimize -->
			<xforms:instance id="optimizeIndex">
				<optimize/>
			</xforms:instance>

			<!-- Instance for Solr commit-->
			<xforms:instance id="sendCommit">
				<commit/>
			</xforms:instance>

			<xforms:instance id="datatype">
				<datatype/>
			</xforms:instance>

			<xforms:instance id="status">
				<status/>
			</xforms:instance>

			<xforms:instance id="ead-collection">
				<exist:result xmlns=""/>
			</xforms:instance>

			<xforms:instance id="vra-collection">
				<exist:result xmlns=""/>
			</xforms:instance>

			<xforms:instance id="current-id">
				<id xmlns=""/>
			</xforms:instance>			

			<!-- get id parameter and set that to be the file to create or delete -->
			<xforms:action ev:event="xforms-model-construct-done">
				<xforms:send submission="load-site-info"/>
				<xforms:send submission="load-ead-collection"/>
				<xforms:send submission="load-vra-collection"/>
			</xforms:action>

			<!-- load site info -->
			<xforms:submission id="load-site-info" serialization="none" method="get"
				action="/exist/rest/db/numishare/site_info.xml" xxforms:username="admin"
				xxforms:password="admin" replace="instance" instance="site-info"/>

			<xforms:submission id="load-ead-collection" serialization="none" method="get"
				action="/exist/rest/db/numishare/ead" xxforms:username="admin"
				xxforms:password="admin" replace="instance" instance="ead-collection"/>

			<xforms:submission id="load-vra-collection" serialization="none" method="get"
				action="/exist/rest/db/numishare/vra" xxforms:username="admin"
				xxforms:password="admin" replace="instance" instance="vra-collection"/>

			<!-- access service to read in pre-transformed solr doc -->
			<xforms:submission id="to-solr" method="get" replace="instance" instance="addIndex"
				serialization="none"
				resource="{instance('site-info')/url}display/{instance('datatype')}/{instance('current-id')}.solr">
				<xforms:message ev:event="xforms-submit-error" level="modal">Error transforming artifact XML to Solr document.</xforms:message>
				<xforms:send ev:event="xforms-submit-done" submission="publish-submission"/>
			</xforms:submission>

			<!-- post instance to Solr -->
			<xforms:submission id="publish-submission"
				action="{instance('site-info')/solr_url}update" ref="instance('addIndex')"
				instance="addIndex" replace="instance" method="post">
				<xforms:message ev:event="xforms-submit-error" level="modal">Data Failed to POST to
					Solr.</xforms:message>
			</xforms:submission>

			<!-- delete from Solr -->
			<xforms:submission id="delete-from-solr" action="{instance('site-info')/solr_url}update"
				ref="instance('delete-all')" instance="delete-all" replace="none" method="post">
				<xforms:send ev:event="xforms-submit-done" submission="submit-commit"/>
				<xforms:setvalue ev:event="xforms-submit-done" ref="instance('status')">All
					artifacts removed from search index.</xforms:setvalue>
				<xforms:message ev:event="xforms-submit-error" level="modal">Data Failed to POST to
					Solr.</xforms:message>
			</xforms:submission>

			<!-- send commit -->
			<xforms:submission id="submit-commit" action="{instance('site-info')/solr_url}update"
				ref="instance('sendCommit')" instance="sendCommit" replace="none" method="post">
				<xforms:action ev:event="xforms-submit-done">
					<xforms:setvalue ref="instance('status')">All artifacts published to search
						index.</xforms:setvalue>
					<xforms:send submission="optimize"/>
				</xforms:action>
			</xforms:submission>

			<!-- send optimize -->
			<xforms:submission id="optimize" action="{instance('site-info')/solr_url}update"
				ref="instance('optimizeIndex')" instance="optimizeIndex" replace="none"
				method="post">
				<xforms:message ev:event="xforms-submit-error" level="modal">Failed to optimize Solr
					index.</xforms:message>
			</xforms:submission>
		</xforms:model>
	</head>
	<body>
		<div class="form">
			<xforms:group ref="instance('status')/text()">
				<div class="success">
					<xforms:output ref="instance('status')"/>
				</div>
			</xforms:group>

			<widget:tabs>
				<!--**************************************** EAD HEADER **********************************-->
				<widget:tab id="publish-tab">
					<widget:label>Publish</widget:label>
					<p> Publish all artifacts in the collection. </p>
					<p>
						<xforms:trigger>
							<xforms:label>Publish All</xforms:label>
							<xforms:action ev:event="DOMActivate">
								<xforms:dispatch target="publish-all-dialog" name="fr-show"/>
							</xforms:action>
						</xforms:trigger>
					</p>
				</widget:tab>
				<widget:tab id="unpublish-tab">
					<widget:label>Unpublish</widget:label>
					<p> Remove all artifacts from the search index (this will not remove the
					artifact metadata from the eXist database). </p>
					<p>
						<xforms:trigger>
							<xforms:label>Unpublish All</xforms:label>
							<xforms:action ev:event="DOMActivate">
								<xforms:dispatch target="unpublish-all-dialog" name="fr-show"/>
							</xforms:action>
						</xforms:trigger>
					</p>
				</widget:tab>
				<widget:tab id="optimize-tab">
					<widget:label>Optimize</widget:label>
					<p> Optimize the index. This may take several moments depending on the size of
					the index. </p>
					<p>
						<xforms:trigger>
							<xforms:label>Optimize</xforms:label>
							<xforms:action ev:event="DOMActivate">
								<xforms:dispatch target="optimize-dialog" name="fr-show"/>
							</xforms:action>
						</xforms:trigger>
					</p>
				</widget:tab>
			</widget:tabs>
		</div>

		<fr:alert-dialog id="publish-all-dialog">
			<fr:label>Publish All Artifacts</fr:label>
			<fr:message> Publish all artifacts? This may take several minutes for large collections. </fr:message>
			<fr:negative-choice/>
			<fr:positive-choice>
				<xforms:action ev:event="DOMActivate">
					<!-- coins: ead -->
					<xforms:setvalue ref="instance('datatype')" value="'ead'"/>
					<xforms:action
						xxforms:iterate="instance('ead-collection')/exist:collection/exist:resource">
						<xxforms:variable name="id" select="substring-before(@name, '.xml')"/>
						<xforms:setvalue ref="instance('current-id')" value="$id"/>
						<xforms:send submission="to-solr"/>
					</xforms:action>
					<!-- non-coin artifacts: vra -->
					<xforms:setvalue ref="instance('datatype')" value="'vra'"/>
					<xforms:action
						xxforms:iterate="instance('vra-collection')/exist:collection/exist:resource">
						<xxforms:variable name="id" select="substring-before(@name, '.xml')"/>
						<xforms:setvalue ref="instance('current-id')" value="$id"/>
						<xforms:send submission="to-solr"/>
					</xforms:action>
					<xforms:send submission="submit-commit"/>
				</xforms:action>

			</fr:positive-choice>
		</fr:alert-dialog>
		
		<fr:alert-dialog id="unpublish-all-dialog">
			<fr:label>Unpublish All Artifacts</fr:label>
			<fr:message>Are you sure you want to unpublish all artifacts?</fr:message>
			<fr:negative-choice/>
			<fr:positive-choice>
				<xforms:action ev:event="DOMActivate">
					<xforms:send submission="delete-from-solr"/>
				</xforms:action>				
			</fr:positive-choice>
		</fr:alert-dialog>
		
		<fr:alert-dialog id="optimize-dialog">
			<fr:label>Optimize Index</fr:label>
			<fr:message>Optimize the index?  This may take several moments.</fr:message>
			<fr:negative-choice/>
			<fr:positive-choice>
				<xforms:action ev:event="DOMActivate">
					<xforms:send submission="optimize"/>
				</xforms:action>				
			</fr:positive-choice>
		</fr:alert-dialog>

		<!--<widget:xforms-instance-inspector id="orbeon-xforms-inspector"
			xmlns:widget="http://orbeon.org/oxf/xml/widget"/>-->
	</body>
</html>
